package com.dapeis.web.wx.controller.impl;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.ag.core.base.Pager;
import com.ag.core.exception.InvalidParamException;
import com.ag.core.exception.SysTipsException;
import com.ag.core.utils.AgAssert;
import com.ag.dao.PagerHelper;
import com.ag.utils.json.AGsonBuilder;
import com.dapeis.core.convert.pager.CommentPagerSearchConvertCriteria;
import com.dapeis.core.utils.StylistConstants;
import com.dapeis.domain.Comment;
import com.dapeis.domain.Order;
import com.dapeis.domain.Plan;
import com.dapeis.domain.ext.CommentExt;
import com.dapeis.domain.ext.OrderExt;
import com.dapeis.service.CollocationPlanService;
import com.dapeis.service.CommentService;
import com.dapeis.service.OrderService;
import com.dapeis.service.UserService;
import com.dapeis.service.order.OrderFlow;
import com.dapeis.web.wx.controller.ICommentController;
import com.dapeis.web.wx.controller.RestController;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

/**
 * @author 作者 E-mail: hzyuanyong@126.com
 * @version 创建时间：2017年5月11日 下午10:19:13 类说明
 */
@Controller
@RequestMapping("/wx/comment")
public class CommentImplController extends RestController implements
	ICommentController {

    @Resource
    OrderService orderService;

    @Resource
    CommentService commentService;
    @Resource
    UserService userService;
    @Resource
    CollocationPlanService planService;

    @Override
    public JsonElement addComment(@RequestParam Long orderId,
	    @RequestParam String score,
	    @RequestParam(required = false) String content,
	    HttpServletRequest request) {
	OrderExt orderExt = orderService.getOrderExtMapper()
		.selectByPrimaryKey(orderId);
	OrderFlow orderFolFlow = orderService.getFlow(orderExt.getType());
	orderFolFlow
		.check(orderExt, StylistConstants.Order.Status.WAIT_COMMENT);
	AgAssert.notNull(orderExt, SysTipsException.class, "查询不到订单 orderId:"
		+ orderId);
	AgAssert.isTrue(Comment.checkScoreValue(score),
		InvalidParamException.class, "分值只能是1~5分");
	boolean result = commentService.addComment(getThreadLocalUserId(),
		orderId, orderExt.getRequirement().getId(), score, content);
	if (result) {//更新订单信息
	    Order order = orderService.selectForLock(orderId);
	    order.setState(orderFolFlow.next(order.getState()));
	    order.setBizFinishTime(new Date().getTime() / 1000);
	    order.setCommentId(commentService.getCommentOrderId(orderId)
		    .getId());
	    orderService.updateByPrimaryKeySelective(order);
	    JsonObject json = success();
		json.add(
			"comment",
			AGsonBuilder.create().toJsonTree(
				commentService.getCommentOrderId(orderId)));
		return json;
	}
	throw new SysTipsException("发表评论失败,请重试 !");
    }

    /**
     * 方案评论
     */
    @Override
    public JsonElement plan(Integer planId, String content,
	    HttpServletRequest request) {
	Plan plan = planService.selectByPrimaryKey(planId);
	AgAssert.notNull(plan, SysTipsException.class, "查询不到方案信息:" + planId);
	AgAssert.notNull(content, SysTipsException.class, "评论内容不能为空!");
	Comment comment = commentService.addCommentPlan(getThreadLocalUserId(),
		planId, content);
	
	CommentExt commentExt = commentService.getCommentExtMapper().selectByPrimaryKey(comment.getId());
	JsonObject json = success();
	json.add("data", AGsonBuilder.create().toJsonTree(commentExt));
	return json;
    }

    @Override
    public JsonElement getCommentByPlan(@RequestParam Integer planId,
	    @RequestParam(required = false) Integer currentPager,
	    HttpServletRequest request) {
	Plan plan = planService.selectByPrimaryKey(planId);
	AgAssert.notNull(plan, SysTipsException.class, "查询不到方案信息:" + planId);
	Pager pager = new Pager();
	pager.init(Comment.class, new CommentPagerSearchConvertCriteria());
	Example example = new Example(Comment.class);
	Criteria criteria = example.createCriteria();
	criteria.andEqualTo(Comment.PROPERTY_PLANID, planId);
	criteria.andEqualTo(Comment.PROPERTY_TYPE,StylistConstants.CommentType.PLAN);
	pager.setExample(example);
	example.orderBy(Comment.PROPERTY_CREATETIME).desc();
	/*取消分页
	if(NumberUtils.isValid(currentPager))
	    pager.setCurrentPage(currentPager);
	    */
	pager.setPageSize(100000);
	commentService.pager(pager);
	PagerHelper.pager(pager, commentService.getCommentExtMapper());
	JsonObject json = success();
	json.add("pager", AGsonBuilder.create().toJsonTree(pager));
	return json;
	
    }
}
